package com.zdyl.fileReplaceTool.utils;

import com.zdyl.fileReplaceTool.Listener.JTextFieldHintListener;
import com.zdyl.fileReplaceTool.Start;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class JFrameUtils extends JFrame {

    JPanel choose = new JPanel();
    JPanel checkBox = new JPanel();
    JPanel path = new JPanel();
    JPanel name = new JPanel();
    JPanel oldStr = new JPanel();
    JPanel newStr = new JPanel();
    ButtonGroup bg = new ButtonGroup();
    //创建单选框
    JRadioButton jrb1 = new JRadioButton("手动输入条件",true);
    JRadioButton jrb2 = new JRadioButton("读取配置文件");
    // 创建复选框
    JCheckBox checkBox01 = new JCheckBox("文件名匹配大小写");
    JCheckBox checkBox02 = new JCheckBox("字符串匹配大小写");

    JLabel pathLabel = new JLabel("目标路径");
    JLabel nameLabel = new JLabel("文件名(模糊)");
    JLabel oldStrLabel = new JLabel("老字符串");
    JLabel newStrLabel = new JLabel("新字符串");

    //创建JTextField，35表示35列，用于JTextField的宽度显示而不是限制字符个数
    JTextField pathField = new JTextField(65);
    JTextField nameField = new JTextField(61);
    JTextField oldStrField = new JTextField(65);
    JTextField newStrField = new JTextField(65);

    JTextArea info = new JTextArea(16,63);
    JScrollPane scrollPane = new JScrollPane();
    JButton button = new JButton("确定");

    //构造函数
    public JFrameUtils(String title) throws IOException {
        //继承父类，
        super(title);
        //设置图标
        URL resource = this.getClass().getResource("/resources/logo.png");
        BufferedImage image = ImageIO.read(resource);
        this.setIconImage(image);

        //内容面板
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());

        info.setEditable(false);

        //添加控件
        bg.add(jrb1);
        bg.add(jrb2);
        choose.add(jrb1);
        choose.add(jrb2);
        checkBox.add(checkBox01);
        checkBox.add(checkBox02);
        path.add(pathLabel);
        path.add(pathField);
        name.add(nameLabel);
        name.add(nameField);
        oldStr.add(oldStrLabel);
        oldStr.add(oldStrField);
        newStr.add(newStrLabel);
        newStr.add(newStrField);
        scrollPane.setViewportView(info);
        contentPane.add(choose);
        contentPane.add(checkBox);
        contentPane.add(path);
        contentPane.add(name);
        contentPane.add(oldStr);
        contentPane.add(newStr);
        contentPane.add(scrollPane);
        contentPane.add(button);
        checkBox02.setSelected(true);

        pathField.addFocusListener(new JTextFieldHintListener(pathField, "例：C:\\Program Files\\test,C:\\Program Files\\config-appsetting"));
        nameField.addFocusListener(new JTextFieldHintListener(nameField, "例：.properties,.xml,.config,.json"));
        oldStrField.addFocusListener(new JTextFieldHintListener(oldStrField, "例：size"));
        newStrField.addFocusListener(new JTextFieldHintListener(newStrField, "例：Cheshire"));

        //按钮点击处理 lambda表达式
        button.addActionListener((e) -> {
            onButtonOk();
        });
    }

    //事件处理
    private void onButtonOk() {
        String path = pathField.getText();//获取输入内容
        String name = nameField.getText();
        String oldStr = oldStrField.getText();
        String newStr = newStrField.getText();

        if (jrb1.isSelected()){
            //判断是否输入了
            if(path.equals("") || name.equals("") || oldStr.equals("") || newStr.equals("")) {
                Object[] options = {"OK ", "CANCEL "};
                JOptionPane.showOptionDialog(null, "您有条件没有输入！", "提示", JOptionPane.DEFAULT_OPTION,
                        JOptionPane.WARNING_MESSAGE,null, options, options[0]);
            }else{
                Start start = new Start();
                info.setText(start.start(path,name,oldStr,newStr,checkBox01.isSelected(),checkBox02.isSelected()));
                JOptionPane.showMessageDialog(this,"使用输入条件替换完成！详细信息请查看日志。");
            }
        } else{
            Start start = new Start();
            info.setText(start.start(checkBox01.isSelected(),checkBox02.isSelected()));
            JOptionPane.showMessageDialog(this,"使用配置文件替换完成！详细信息请查看日志。");
        }

    }

}
